package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallTransportMapper;
import com.hmall.mapper.SysUserMapper;
import com.hmall.model.MallTransport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName TransportService
 * @Description //
 * @Author zhoutao
 * @Date 2018/10/23 10:53
 * @Version 2.0
 **/
@Service
public class TransportService {


    @Autowired
    SysUserMapper userMapper;

    @Autowired
    MallTransportMapper mallTransportMapper;

    public ServerResponse<List<MallTransport>> getTransportList(Integer userId) {

        List<MallTransport> mallTransportList = mallTransportMapper.selectByUserId(userId);
        return ServerResponse.createBySuccess(mallTransportList);
    }

    public ServerResponse createTransport(Integer userId, String linkmanName, String linkmanTel, String plateNumber, String license, Integer defaultCheck, String remark) {

        changeDefaultTransport(userId, defaultCheck);
        MallTransport mallTransport = new MallTransport();
        mallTransport.setUserId(userId);
        mallTransport.setLinkmanName(linkmanName);
        mallTransport.setLinkmanTel(linkmanTel);
        mallTransport.setPlateNumber(plateNumber);
        mallTransport.setLicense(license);
        mallTransport.setDefaultCheck(defaultCheck);
        mallTransport.setRemark(remark);
        try {
            mallTransportMapper.insert(mallTransport);
            return ServerResponse.createBySuccess("创建成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("创建失败");
        }

    }

    private void changeDefaultTransport(Integer userId, Integer defaultCheck){

        if(1 == defaultCheck) {
            mallTransportMapper.updateResetDefaultCheck(userId);
        }

    }

    public ServerResponse deleteTransport(Integer transportId) {

        try{
            mallTransportMapper.deleteByPrimaryKey(transportId);
            return ServerResponse.createBySuccess("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("删除失败");
        }
    }

    public ServerResponse updateTransport(Integer userId, Integer transportId, String linkmanName, String linkmanTel, String plateNumber, String license, Integer defaultCheck, String remark) {

        changeDefaultTransport(userId, defaultCheck);
        MallTransport mallTransport = new MallTransport();
        mallTransport.setTransportId(transportId);
        mallTransport.setRemark(remark);
        mallTransport.setDefaultCheck(defaultCheck);
        mallTransport.setLicense(license);
        mallTransport.setLinkmanTel(linkmanTel);
        mallTransport.setLinkmanName(linkmanName);
        mallTransport.setPlateNumber(plateNumber);
        try {
            mallTransportMapper.updateTransport(mallTransport);
            return ServerResponse.createBySuccess("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("更新失败");
        }
    }

    public ServerResponse setDefaultTransport(Integer userId, Integer defaultCheck, Integer transportId) {

        changeDefaultTransport(userId, defaultCheck);
        try{
            mallTransportMapper.updateOneDefaultCheck(transportId, defaultCheck);
            return ServerResponse.createBySuccess("设置默认地址成功");
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("设置默认地址失败");
        }
    }
}
